import {reqSongDetail,reqLyric} from "@/http/api";
import {formatTimeToSec} from "@/utils/formatDate";
export default {
    namespaced: true,//开启命名空间
    state: {
        songs: [],
        lyric:'',
        songLyric: [],
        lyrics:[],
        lyricList:''
    },
    mutations: {
        GETBANNER(state,data){
            state.songs =  data.songs[0]
        },
        GETSONGLYRIC(state,data){
            state.lyric = data.lrc.lyric


            let lyric = data.lrc.lyric
            let result = [];
            let re = /\[([^\]]+)\]([^[]+)/g;
            lyric.replace(re, ($0, $1, $2) => {
                result.push({
                    time: formatTimeToSec($1),
                    lyric: $2
                });
            });
            state.songLyric = result

            let list = []
            state.songLyric.forEach(item=>{
                list.push(item.lyric.replace('\n','<br>'))
            })
            state.lyrics = list
            state.lyricList = state.lyrics.join('')
        },

    },
    actions: {
        async getSongDetail({commit},id) {
            let result = await reqSongDetail(id)
            commit('GETBANNER',result.data)
        },
        async getSongLyric({commit},id) {
            let result = await reqLyric(id)
            commit('GETSONGLYRIC',result.data)
        },
    },
    getters: {}
}
